package org.jeecg.modules.yw.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.yw.entity.LostSome;
import org.jeecg.modules.yw.entity.Lostsomeapprove;
import org.jeecg.modules.yw.entity.Messages;
import org.jeecg.modules.yw.entity.Requestapprove;
import org.jeecg.modules.yw.service.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DESUtil;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.yw.vo.LostsomeapproveVo;
import org.jeecg.modules.yw.vo.ReportInfoQueryListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 报损审批表
 * @Author: jeecg-boot
 * @Date:   2025-04-15
 * @Version: V1.0
 */
@Api(tags="报损审批表")
@RestController
@RequestMapping("/yw/lostsomeapprove")
@Slf4j
public class LostsomeapproveController extends JeecgController<Lostsomeapprove, ILostsomeapproveService> {
	@Autowired
	private ILostsomeapproveService lostsomeapproveService;
	@Autowired
	private LostSomeService lostsomeService;
	@Autowired
	private EnterPriseService enterPriseService;
	@Autowired
	private OrderService orderService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;
	 @Autowired
	 private MessagesService messagesService;
	/**
	 * 分页列表查询
	 *
	 * @param id
	 * @param pageNo
	 * @param pageSize
	 * @param
	 * @return
	 */
	//@AutoLog(value = "报损审批表-分页列表查询")
	@ApiOperation(value="报损审批表-分页列表查询", notes="报损审批表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Lostsomeapprove>> queryPageList(
								   @RequestParam(name="id", required = false) String id,
								   @RequestParam(name="name", required = false) String name,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<Lostsomeapprove> queryWrapper = new QueryWrapper<>();
		if(StringUtils.isNotBlank(id)) {
			queryWrapper.eq("id", id);
		}
		Page<Lostsomeapprove> page = new Page<Lostsomeapprove>(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roleIds = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
		if (!roleIds.contains(CommonUtils.ADMIN)) {
			String area=sysUser.getArea();
			if(org.apache.commons.lang3.StringUtils.isNotBlank(area)){
				String[] areas=area.split(",");
				StringBuffer st=new StringBuffer();
				for (int i = 0; i < areas.length; i++) {
					st.append("\""+areas[i]+"\",");
				}
				st=st.deleteCharAt(st.length()-1);
				String sql="(select id from enterprise where city_code in ("+st.toString()+") " +
						"or (province_code in  ("+st.toString()+") and city_code is null))";
				sql="(select id from lostsome where enterPriseId in("+sql+"))";
				queryWrapper.apply("lostsomeid in"+sql+"");
			}else{
				IPage<Lostsomeapprove> result =new Page<>();
				return Result.OK(result);
			}
		}
		if(StringUtils.isNotBlank(name)){
			String sql="select id from enterprise where name like '%"+name+"%' and delflag='0'";
			sql="(select id from lostsome where enterPriseId in("+sql+"))";
			queryWrapper.apply("lostsomeid in"+sql+"");
		}
		queryWrapper.orderByDesc("createtime");
		IPage<Lostsomeapprove> pageList = lostsomeapproveService.page(page, queryWrapper);
		for (Lostsomeapprove item : pageList.getRecords()){
			LostSome lostSome = lostsomeService.getById(item.getLostsomeid());
			item.setWorkname(enterPriseService.getById(lostSome.getEnterPriseId()).getName());
			item.setOrderno(orderService.getById(lostSome.getOrderId()).getOrderNo());
			item.setLostSome(lostsomeService.getById(item.getLostsomeid()));
		}
		return Result.OK(pageList);
	}
	 @ApiOperation(value="报损审批表-通过id通过", notes="报损审批表-通过id通过")
	 @GetMapping(value = "/passById")
	 public Result passById(@RequestParam(name="id",required=true) String id, HttpServletRequest request) {
		 Lostsomeapprove lostsomeapprove1 = lostsomeapproveService.getById(id);
		 lostsomeapprove1.setApprove("1");
		 lostsomeapprove1.setRemark("");
		 lostsomeapprove1.setApproveman(UserUtil.getUserId(request, sysBaseAPI));
		 lostsomeapprove1.setApprovetime(DateUtils.getCurrentDate());
		 lostsomeapproveService.updateById(lostsomeapprove1);
		 String lid=lostsomeapprove1.getLostsomeid();
		 LostSome lostSome=lostsomeService.getById(lid);
		 lostSome.setLostStatus(1);
		 lostsomeService.updateById(lostSome);
		 Messages m = new Messages();
		 m.setId(CommonUtils.getID());
		 m.setOrderId(lostSome.getOrderId());
		 m.setOrderNo(lostSome.getOrderNo());
		 m.setMsgType(2);
		 m.setNormalMsgType(5);
		 m.setCurrentUserId(lostSome.getEnterPriseId());
		 m.setDelFlag(0);
		 m.setRemark("您的报损已通过");
		 m.setMsgTime(DateUtils.getCurrentTimeDateStr());
		 messagesService.save(m);
		 return Result.OK();
	 }
	 @ApiOperation(value="报损审批表-通过id不通过", notes="报损审批表-通过id不通过")
	 @GetMapping(value = "/unpassById")
	 public Result unpassById(@RequestParam(name="id",required=true) String id,
							  @RequestParam(value = "remark",required = false) String remark,
							  HttpServletRequest request) {
		 Lostsomeapprove lostsomeapprove1 = lostsomeapproveService.getById(id);
		 lostsomeapprove1.setApprove("2");
		 lostsomeapprove1.setRemark(remark);
		 lostsomeapprove1.setApproveman(UserUtil.getUserId(request, sysBaseAPI));
		 lostsomeapprove1.setApprovetime(DateUtils.getCurrentDate());
		 lostsomeapproveService.updateById(lostsomeapprove1);
		 String lid=lostsomeapprove1.getLostsomeid();
		 LostSome lostSome=lostsomeService.getById(lid);
		 lostSome.setLostStatus(2);
		 lostsomeService.updateById(lostSome);
		 Messages m = new Messages();
		 m.setId(CommonUtils.getID());
		 m.setOrderId(lostSome.getOrderId());
		 m.setOrderNo(lostSome.getOrderNo());
		 m.setMsgType(2);
		 m.setNormalMsgType(5);
		 m.setCurrentUserId(lostSome.getEnterPriseId());
		 m.setDelFlag(0);
		 m.setRemark("您的报损未通过");
		 m.setMsgTime(DateUtils.getCurrentTimeDateStr());
		 messagesService.save(m);
		 return Result.OK();
	 }
	/**
	 *   添加
	 *
	 * @param lostsomeapprove
	 * @return
	 */
	@AutoLog(value = "报损审批表-添加")
	@ApiOperation(value="报损审批表-添加", notes="报损审批表-添加")
	//@RequiresPermissions("org.jeecg.modules:lostsomeapprove:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Lostsomeapprove lostsomeapprove) {
		lostsomeapproveService.save(lostsomeapprove);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lostsomeapprove
	 * @return
	 */
	@AutoLog(value = "报损审批表-编辑")
	@ApiOperation(value="报损审批表-编辑", notes="报损审批表-编辑")
	//@RequiresPermissions("org.jeecg.modules:lostsomeapprove:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Lostsomeapprove lostsomeapprove) {
		lostsomeapproveService.updateById(lostsomeapprove);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报损审批表-通过id删除")
	@ApiOperation(value="报损审批表-通过id删除", notes="报损审批表-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:lostsomeapprove:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		lostsomeapproveService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报损审批表-批量删除")
	@ApiOperation(value="报损审批表-批量删除", notes="报损审批表-批量删除")
	//@RequiresPermissions("org.jeecg.modules:lostsomeapprove:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lostsomeapproveService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "报损审批表-通过id查询")
	@ApiOperation(value="报损审批表-通过id查询", notes="报损审批表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Lostsomeapprove> queryById(@RequestParam(name="id",required=true) String id) {
		Lostsomeapprove lostsomeapprove = lostsomeapproveService.getById(id);
		if(lostsomeapprove==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lostsomeapprove);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lostsomeapprove
    */
    //@RequiresPermissions("org.jeecg.modules:lostsomeapprove:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Lostsomeapprove lostsomeapprove) {
        return super.exportXls(request, lostsomeapprove, Lostsomeapprove.class, "报损审批表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("lostsomeapprove:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Lostsomeapprove.class);
    }
}
